package com.topup.Controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.topup.pojo.Student;
import com.topup.pojo.User;
import com.topup.service.StudentService;
import com.topup.until.Result;

import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("stu")
public class StudentController {
	
	@Autowired
	private StudentService servicel;
	
	@RequestMapping("/loginstu")
	@ResponseBody
	public Result selectStu(HttpServletRequest request) {
		System.out.println("dl");
		 	Result r=new Result();
	        String name1=request.getParameter("username");
	        String pwd1=request.getParameter("userpwd");
	        Student student =      servicel.selectStu(new Student(name1, null, pwd1, null, null));
		 System.out.println(student);
			if(student!=null){
	            r.setStatus("0");
	            request.getSession().setAttribute("stu",student);
	            request.setAttribute("stu", student);
	          }else{
	            r.setStatus("1");
	        }
			return r;
	}
	
	@RequestMapping("selAll")
	public String selectWo(HttpServletRequest request) {
		Student s = (Student)request.getSession().getAttribute("stu");
			request.setAttribute("ss", s);
		return "StudentCenten";
	}





	@RequestMapping("stuall")
	public String selecta(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("sfaewf222");
		List<Student> list= servicel.selecta();
		request.setAttribute("ss",list);
		return "studentinfo";
	}



	@RequestMapping("updatestu")
	@ResponseBody
	public Result updatestu(HttpServletRequest request){
		System.out.println("sfaewf");
		Result r= new Result();
		String num= request.getParameter("num");
		
		int nn = Integer.parseInt(num);
		String name= request.getParameter("name");
		 String sex= request.getParameter("sex");
		 String classes= request.getParameter("classes");
		 String department= request.getParameter("department");
		 String email= request.getParameter("email");
		 String phone= request.getParameter("phone");
		 Student s = (Student)request.getSession().getAttribute("stu");
			
			Student stt = new Student(s.getId(), nn, name, sex, classes, department, email, phone);
				int result = servicel.update(stt);
				if(result>0) {
					r.setStatus("0");
					request.getSession().invalidate();
				}else {
					r.setStatus("1");
				}
			return r;
	}
	
	
}
